﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;

namespace Demo.Model
{
    [MetadataType(typeof(PersonMetadata))]
    public class Person : INotifyPropertyChanged, IDataErrorInfo
    {
        private Nullable<int> _id;
        private string _name;
        private DateTime _birthday;
        private Department _department;
        private ImageSource _image;

        public Nullable<int> Id
        {
            get { return _id; }
            set { _id = value; this.OnPropertyChanged("Id"); }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; this.OnPropertyChanged("Name"); }
        }

        public DateTime Birthday
        {
            get { return _birthday; }
            set { _birthday = value; this.OnPropertyChanged("Birthday"); }
        }

        public Department Department
        {
            get { return _department; }
            set { _department = value; this.OnPropertyChanged("Department"); }
        }

        public ImageSource Image
        {
            get { return _image; }
            set { _image = value; this.OnPropertyChanged("Image"); }
        }

        #region IDataErrorInfo Members

        [Display(AutoGenerateField=false)]
        public string Error
        {
            get { return this[""]; }
        }

        [Display(AutoGenerateField = false)]
        public string this[string columnName]
        {
            get
            {
                string error = string.Empty;
                if (string.IsNullOrEmpty(columnName) ||
                    columnName == "Id")
                {
                    if (this.Id < 1)
                        error += Properties.Resources.PersonValidationIdMandatory + Environment.NewLine;
                }
                if (string.IsNullOrEmpty(columnName) ||
                    columnName == "Name")
                {
                    if (string.IsNullOrEmpty(this.Name))
                        error += Properties.Resources.PersonValidationNameMandatory + Environment.NewLine;
                }
                if (string.IsNullOrEmpty(columnName) ||
                    columnName == "Department")
                {
                    if (this.Department == null)
                        error += Properties.Resources.PersonValidationDepartmentMandatory + Environment.NewLine;
                }
                return error;
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }

    public class Department
    {
        public string Name { get; set; }
    }
}
